﻿using System;
using System.Configuration;
using System.Collections.Generic;

using System.Text;

namespace SqlMetalBuilder
{
    [Serializable]
    class SqlMetal
    {
        public SqlMetalConnectionOptions ConnectionOption;
        public AuthenticationType AuthenticationType;
        public bool IsServerNameDefault;
        public string ServerName;
        public string UserName;
        public string Password;
        public string Database;
        public string ConnectionString;
        public string InputFile;
        public string CodeOutputFile;
        public string MapOutputFile;
        public string DbmlOutputFile;
        public bool IsCodeOutputFile = false;
        public bool IsCodePreview = false;
        public bool IsMapOutputFile = false;
        public bool IsDbmlOutputFile = false;
        public bool IsDbmlPreview = false;


        public int TimeOut;
        public bool IsDefaultTimeOut;
        public string NameSpace;
        public string DataContext;
        public string BaseClass;
        public Language enmLanguage;
        public Serialzation enmSerialzation;
        public bool IsPuralize;

        public bool IncludeDatabaseStoredProcedure;
        public bool IncludeDatabaseViews;
        public bool IncludeDatabaseFunctions;

        static public string GetExeLocation()
        {
            return ConfigurationManager.AppSettings["SqlMetalFileLocation"];
            
        }
        public string GetArguments()
        {
            StringBuilder objCommandStringBuilder = new StringBuilder();
            if (this.ConnectionOption == SqlMetalConnectionOptions.Database)
            {
                if(!IsServerNameDefault)
                    objCommandStringBuilder.Append(" /server:" + ServerName);
                if (AuthenticationType == AuthenticationType.SQLServer)
                {
                    objCommandStringBuilder.Append(" /user:" + UserName);
                    objCommandStringBuilder.Append(" /password:" + Password);
                }
                objCommandStringBuilder.Append(" /database:" + Database);
            }
            else if(this.ConnectionOption == SqlMetalConnectionOptions.ConnectionString)
                objCommandStringBuilder.Append(" /conn:" + ConnectionString);
            if (!IsDefaultTimeOut)
            {
                objCommandStringBuilder.Append(" /timeout:" + TimeOut.ToString());
            }

            if (IsDbmlOutputFile)
            {
                if(IsDbmlPreview)
                    objCommandStringBuilder.Append(" /dbml:\"\"");
                else
                    objCommandStringBuilder.Append(" /dbml:\"" + this.DbmlOutputFile + "\"");
            }
            else if (IsCodeOutputFile)
            {
                if (IsCodePreview)
                    objCommandStringBuilder.Append(" /code:\"\"");
                else
                {
                    objCommandStringBuilder.Append(" /code:\"" + this.CodeOutputFile + "\"");
                    if (IsMapOutputFile)
                        objCommandStringBuilder.Append(" /map:\"" + this.MapOutputFile + "\"");
                }
            }

            if (NameSpace != null && NameSpace.Trim().Length != 0)
                objCommandStringBuilder.Append(" /namespace:" + this.NameSpace);
            if (DataContext != null && DataContext.Trim().Length != 0)
                objCommandStringBuilder.Append(" /context:" + this.DataContext);
            if (BaseClass != null && BaseClass.Trim().Length != 0)
                objCommandStringBuilder.Append(" /entitybase:" + this.BaseClass);

            if (this.enmSerialzation == Serialzation.None)
                objCommandStringBuilder.Append(" /serialization:None");
            else if (enmSerialzation == Serialzation.Unidirectional)
                objCommandStringBuilder.Append(" /serialization:Unidirectional");

            if (enmLanguage == Language.CSharp)
                objCommandStringBuilder.Append(" /language:csharp");
            else if (enmLanguage == Language.VBDotNet)
                objCommandStringBuilder.Append(" /language:vb");

            if(IsPuralize)
                objCommandStringBuilder.Append(" /pluralize");

            if(IncludeDatabaseViews)
                objCommandStringBuilder.Append(" /views");
            if (IncludeDatabaseFunctions) 
                objCommandStringBuilder.Append(" /functions");
            if (IncludeDatabaseStoredProcedure) 
                objCommandStringBuilder.Append(" /sprocs");  

            if (this.ConnectionOption == SqlMetalConnectionOptions.InputFile)
            {
                objCommandStringBuilder.Append(" \"" + InputFile+"\"");
            }
            return objCommandStringBuilder.ToString();
        }
    }
    enum SqlMetalConnectionOptions
    {
        Database,
        ConnectionString,
        InputFile
    }
    enum Language
    {
        Default,
        CSharp,
        VBDotNet
    }
    enum Serialzation
    {
        Default,
        None, 
        Unidirectional  
    }
    //enum Puralization
    //{
    //    Plural,
    //    Singular
    //}
    enum AuthenticationType
    {
        Windows,
        SQLServer
    }
}
